<template>
  <!-- 企业审核 -->
  <el-dialog
    v-dialogDrag
    title="企业审核"
    :visible.sync="dialogVisible"
    class="dialogStyleMiddle"
    :before-close="cancel"
    :close-on-click-modal="false"
    append-to-body
  >
    <el-row>
      <el-form ref="form" label-width="80px" :rules="rules" :model="formData">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="企业名称" prop="name">
              <el-input v-model="formData.name" disabled size="medium" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业类型" prop="sysEnterpriseType">
              <el-select :value="formData.sysEnterpriseType" size="medium" disabled placeholder="">
                <el-option
                  v-for="item in typeOptions"
                  :key="item.typeNum"
                  :label="item.typeName"
                  :value="item.typeNum"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="" prop="userdName">
              <label slot="label" v-html="`曾&ensp;用&ensp;名`" />
              <el-input v-model="formData.userdName" disabled size="medium" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="信用代码" prop="creditCodePic">
              <el-input v-model="formData.creditCodePic" disabled size="medium" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="详细地址" prop="address">
              <el-input v-model="formData.address" disabled size="medium" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位法人" prop="legalperson">
              <el-input v-model="formData.legalperson" disabled size="medium" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="" prop="contacts">
              <label slot="label" v-html="`联&ensp;系&ensp;人`" />
              <el-input v-model="formData.contacts" disabled size="medium" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="formData.phone" disabled size="medium" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="通过审核" prop="checkStatus">
              <el-radio-group v-model="formData.checkStatus" :disabled="status === '3'">
                <el-radio label="0">是</el-radio>
                <el-radio label="1">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col v-if="status !== '3'" :span="24">
            <el-form-item v-if="formData.checkStatus !=='0'" :key="1" label="审核原因" :prop="'reason'">
              <el-input
                v-model="formData.reason"
                type="textarea"
                :rows="5"
                :maxlength="500"
              />
            </el-form-item>
            <el-form-item v-if="formData.checkStatus ==='0'" :key="2" label="审核原因">
              <el-input
                v-model="formData.reason"
                type="textarea"
                :rows="5"
                :maxlength="500"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="cancel">取 消</el-button>
      <el-button v-if="status !== '3'" size="medium" type="primary" @click="submitForm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { listNoPermission } from '@/api/superviseBaseInfo/typeManage/organizationType'
import { enterpriseAudit, enterpriseDetail } from '@/api/PersonalOffice/approvalManage/enterpriseApproval'
export default {
  name: 'CheckDialog',
  props: {
    readonly: {
      type: Boolean,
      default: true
    }
  },
  data() {
    const checkStatus = (rule, value, callback) => {
      if (value !== '0' && value !== '1') {
        callback(new Error('请选择'))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false, // 弹框显隐
      /* 表单 start */
      formData: {
        id: '',
        name: '', // 企业名称
        userdName: '', // 曾用名
        creditCodePic: '', // 企业信用代码
        unitCreaditCodePic: '', // 信用代码扫描件
        sysEnterpriseType: '', // 企业类型
        address: '', // 详细地址
        legalperson: '', // 单位法人
        contacts: '', // 联系人
        phone: '', // 联系人电话
        checkStatus: '0', // 审核状态
        reason: '' // 审核备注
      },
      rules: {
        reason: [{ required: true, message: '审核原因不能为空', trigger: 'blur' }],
        checkStatus: [{ required: true, message: '审核不能为空', validator: checkStatus, trigger: 'change' }]
      },
      typeOptions: [],
      status: '' // 企业目前状态
    }
    /* 表单 end */
  },
  methods: {
    // 获取企业类型
    async getSupperType() {
      const query = { typeVal: 1 }
      const { data: { rows }} = await listNoPermission(query)
      this.typeOptions = [{ typeNum: '', typeName: '全部' }, ...rows]
    },
    /* 新增/编辑弹层 start */
    // 初始化信息
    handleAdd(id) {
      this.dialogVisible = true
      this.resetForm('form')
      this.getSupperType()
      this.$nextTick(() => {
        this.enterpriseDetail(id)
      })
    },
    // 获取企业详情
    async enterpriseDetail(enterpriseId) {
      const { data: { id, name, userdName, creditCodePic, sysEnterpriseType, unitCreaditCodePic, address, legalperson, contacts, phone, checkStatus, reason }} = await enterpriseDetail(enterpriseId)
      this.formData = { ...this.formData, id, name, userdName, creditCodePic, sysEnterpriseType, unitCreaditCodePic, address, legalperson, contacts, phone, reason }
      this.status = checkStatus
      // if (checkStatus !== '2') this.formData.checkStatus = checkStatus
    },
    // 确认
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          const { sysEnterpriseType, reason, checkStatus, id: companyId } = this.formData
          const data = { companyId, reason, checkStatus, sysEnterpriseType }
          const { message } = await enterpriseAudit(data)
          this.msgSuccess(message)
          this.$parent.getList()
          this.cancel()
        }
      })
    },
    // 关闭
    cancel() {
      this.dialogVisible = false
    }
    /* 新增/编辑弹层 end */
  }
}
</script>

